import sys,os;
import hashlib
def cod_file2md5(filepathname):
    umd5str=hashlib.md5();
    ufile = open(unicode(filepathname,'utf8'), 'rb');
    umd5str.update(ufile.read());
    uoutstr=str(umd5str.hexdigest());
    ufile.close();
    return uoutstr;

def get_File_path(rootdir):
    dirs = [];
    for parent,dirnames,filenames in os.walk(rootdir):
        #for dirname in  dirnames:
            #print "parent is:" + parent;
            #print  "dirname is:" + dirname;
            #dirs.append(dirname);
        for filename in filenames:
            filePath = os.path.join(parent,filename);
            dirs.append(filePath.replace("\\","/"));
    return dirs;

def save_file(fileName,fileInfo):
    endFile_object = open(fileName,"w");
    endFile_object.write(fileInfo);
    endFile_object.close();

    

if __name__ == "__main__":
    Version = raw_input("Version:");
    endStr = "local " + 't' + " = {";
    endStr += "\r\tversion = %s%s%s," % ('"',Version,'"');
    endStr += "\r\tcrcInfo = {";
    resPath = get_File_path("res");
    srcPath = get_File_path("src");
    resPath.extend(srcPath)
    for f in resPath:
        fileSize = os.path.getsize(f);
        fileMd5 = cod_file2md5(f);
        print("file:%s ok" % f);
        endStr += "\r\t\t\t\t[%s%s%s] = {md5 = %s%s%s,size = %s}," % ('"',f,'"','"',fileMd5,'"',fileSize);
    endStr = endStr[:-1];
    endStr += "\r\n\t\t\t}\r\n} \r\nreturn " + 't' + ";";
    save_file("cdn/crcList.lua",endStr);
    print "OK!!!!";
    os.system("pause");

